﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using LibCommon;
using LibRTC.Core;

namespace LibGUI.MainForm.UserList
{
    class BaseUserTreeView : TreeView, IUserTreeView
    {
        private static ILogger LOGGER = LoggerFactory.GetLogger("BaseUserTreeView", null);

        protected static readonly RTCSystem _rtcSystem = RTCSystem.GetInstance();

        public BaseUserTreeView() {
             
        }

        public virtual void AddGroup(string group) {
            Debug.Assert(null != group);

            foreach (TreeNode node in Nodes) {
                if(node.Name.Equals(group)) {
                    LOGGER.LogError("Group = {} exist "+group);
                    return;
                }
            }

            Nodes.Add(group,group);
        }

        public virtual void RemoveGroup(string group)
        {
            Debug.Assert(null != group);

            TreeNode foundNode = null;

            foreach (TreeNode node in Nodes) {
                if(node.Name.Equals(group)) {
                    foundNode = node;
                    break;
                }
            }

            if(null != foundNode) {
                Nodes.Remove(foundNode);
            }
            else {
                LOGGER.LogError("Can not found group : "+group);
            }

        }

        public virtual void AddUserNode(string group, BaseUserNode userTreeNode)
        {

            Debug.Assert(null != group);
            Debug.Assert(null != userTreeNode);

            bool foundGroup = false;
            BaseUserNode foundUserNode = null;

            BeginUpdate();

            // check whether the group exist
            foreach (TreeNode node in Nodes) {
                if(node.Name.Equals(group)) {
                    foundGroup = true;
                    // check whether there is an duplicate node
                    foreach (TreeNode node2 in node.Nodes)
                    {
                        BaseUserNode userNode = node2 as BaseUserNode;
                        Debug.Assert(null != userNode);

                        if (userNode.Equals(userTreeNode))
                        {
                            foundUserNode = userNode;
                            LOGGER.LogError("Duplicate UserNode, Group : "+group+" UserNode : "+userNode.ToString());
                            break;
                        }
                    }

                    // if did not find user, add it
                    if (null == foundUserNode)
                    {
                        node.Nodes.Add(userTreeNode);
                    }

                    break;
                }
            }

            if(!foundGroup) {
                LOGGER.LogError("Can not find Group : " + group + " for UserNode : " + userTreeNode.ToString());                
            }

            EndUpdate();

        }

        public virtual void RemoveUserNode(string group, BaseUserNode userTreeNode)
        {
            Debug.Assert(null != group);
            Debug.Assert(null != userTreeNode);

            bool foundGroup = false;
            BaseUserNode foundUserNode = null;

            BeginUpdate();

            // check whether the group exist
            foreach (TreeNode node in Nodes)
            {
                if (node.Name.Equals(group))
                {
                    foundGroup = true;
                    // check whether there is an duplicate node
                    foreach (TreeNode node2 in node.Nodes)
                    {
                        BaseUserNode userNode = node2 as BaseUserNode;
                        Debug.Assert(null != userNode);

                        if (userNode.Equals(userNode))
                        {
                            foundUserNode = userNode;
                            break;
                        }
                    }

                    // if did not find user, add it
                    if (null == foundUserNode)
                    {
                        LOGGER.LogError("Duplicate Group : " + group + " UserNode : " + userTreeNode.ToString());
                    }
                    else {
                        Debug.Assert(null != foundUserNode);
                        node.Nodes.Remove(foundUserNode);
                    }
                    
                    break;
                }
            }

            if (!foundGroup)
            {
                LOGGER.LogError("Can not find Group : " + group + " for UserNode : " + userTreeNode.ToString());
            }

            EndUpdate();
        }

        public virtual BaseUserNode RemoveUserNode(string userURI)
        {
            Debug.Assert(null != userURI);
            
            BaseUserNode foundNode = null;

            BeginUpdate();

            foreach (TreeNode node in Nodes) {

                foreach (TreeNode node2 in node.Nodes)
                {
                    BaseUserNode baseNode = node2 as BaseUserNode;
                    Debug.Assert(null != baseNode);

                    if (baseNode.UserURI.Equals(userURI)) {
                        foundNode = baseNode;
                        break;
                    }
                }

                if(null != foundNode) {
                    node.Nodes.Remove(foundNode);
                }
            }

            EndUpdate();

            if(null == foundNode) {
                LOGGER.LogError("Can not find UserNode : "+userURI);
            }

            return foundNode;

        }
    }
}
